//
//  ZGWWebViewController.m
//  1gene
//
//  Created by 张国文 on 16/7/12.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "ZGWWebViewController.h"

@interface ZGWWebViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *myWebView;

@property (nonatomic, strong) UIButton *liJiGouMai; // 立即购买

@property (nonatomic, strong) NSTimer *myTimer; // 检查请求是否超时
@property (nonatomic, assign) NSInteger numberOfTime; // 存储请求时间

@end

@implementation ZGWWebViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden=NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
    [self.myTimer invalidate];
    self.numberOfTime = 0;
    [super viewWillDisappear:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的xiugai
    [self changeNavigation];
    // 检查网络并添加数据源
    [self searchNetAndAddData];
    
}

// navi的修改
- (void)changeNavigation
{
    self.navigationItem.title = @"壹基因";
    [self.navigationController.navigationBar setTranslucent:NO];
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 检查网络并建立数据源
- (void)searchNetAndAddData
{
    if ([[[NetStatusHandle shareHandle] getNetStatus] isEqualToString:@"YES"]) {
        // 说明有网络
        // 布局
       [self addMySubView];
            // 说明不是示例报告
            // 建立计时器，计算是否超时
            self.myTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(goTimeOnZGWWebView:) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];
            self.numberOfTime = 0;
            // 开始动画
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
        
        
        
        
    } else {
        NSLog(@"没有网络");
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kNoNetNoRefresh ToView:self];
    }
}

#pragma mark - 计时器方法
- (void)goTimeOnZGWWebView:(NSTimer *)timer
{
    self.numberOfTime++;
    NSLog(@"网页请求时间= %ld", self.numberOfTime);
    if (self.numberOfTime > kZGWQingQiuChaoShi) {
        NSLog(@"网页请求超时");
        self.numberOfTime = 0;
        [self.myTimer invalidate];
        // 关闭动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kQingQiuChaoShiNoReFresh ToView:self];
    }
}

#pragma mark - 布局
- (void)addMySubView
{
    self.myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.myWebView.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.httpUrlString]];
    [self.myWebView loadRequest:request];
    [self.view addSubview:_myWebView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"加载成功");
    webView.scrollView.bounces = NO;
    webView.scrollView.showsVerticalScrollIndicator = NO;
    // 关闭动画
    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
    
    // 暂停计时器
    [self.myTimer invalidate];
    self.numberOfTime = 0;
    
    if (self.isShowBuybutton == YES) {
        self.liJiGouMai = [UIButton buttonWithType:UIButtonTypeSystem];
        self.liJiGouMai.frame = CGRectMake(0, kScreenHeight - 64 - 57 * kAdjustParamater, kScreenWidth , 57 * kAdjustParamater);
        self.liJiGouMai.backgroundColor = [UIColor colorWithRed:43 / 255.0 green:142 / 255.0 blue:164 / 255.0 alpha:1];
        //    if ([[self.onLineProductMessageDic valueForKey:@"is_promotion"] isEqualToString:@"1"]) {
        //        // 说明促销
        //        NSString *cuXiaoJia = [self.onLineProductMessageDic valueForKey:@"promotion_price"];
        //        [self.liJiGouMai setTitle:[NSString stringWithFormat:@"￥ %@立即购买%@套餐", cuXiaoJia, self.onLineProductName] forState:UIControlStateNormal];
        //    } else if ([[self.onLineProductMessageDic valueForKey:@"is_promotion"] isEqualToString:@"0"]) {
        //        // 说明不促销
        //        NSString *yuanJia = [self.onLineProductMessageDic valueForKey:@"price"];
        //        [self.liJiGouMai setTitle:[NSString stringWithFormat:@"￥ %@立即购买%@套餐", yuanJia, self.onLineProductName] forState:UIControlStateNormal];
        //    }
        
        [self.liJiGouMai setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.liJiGouMai.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k18Big];
        [self.liJiGouMai addTarget:self action:@selector(liJiGouMaiOnLineProduct:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_liJiGouMai];
    }
  
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    // 关闭动画
    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];

    // 暂停计时器
    [self.myTimer invalidate];
    self.numberOfTime = 0;
    
    NSLog(@"网页加载错误");
    
    [webView removeFromSuperview];
    
    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addTopGrayAlertNoImageWithContent:@"网页加载失败，请重新进入" toView:self];
    if (self.isShowBuybutton == YES) {
        self.liJiGouMai = [UIButton buttonWithType:UIButtonTypeSystem];
        self.liJiGouMai.frame = CGRectMake(0, kScreenHeight - 64 - 57 * kAdjustParamater, kScreenWidth , 57 * kAdjustParamater);
        self.liJiGouMai.backgroundColor = [UIColor colorWithRed:43 / 255.0 green:142 / 255.0 blue:164 / 255.0 alpha:1];
        [self.liJiGouMai setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.liJiGouMai.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k18Big];
        [self.liJiGouMai addTarget:self action:@selector(liJiGouMaiOnLineProduct:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_liJiGouMai];
    }
}

#pragma mark - 立即购买
- (void)liJiGouMaiOnLineProduct:(UIButton *)button
{
    NSLog(@"立即购买");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
